//
//  MenstrualMessage.m
//  uteped_sdk
//
//  Created by mateng on 2024/2/20.
//

#import "MenstrualMessage.h"

#define PATH                @"menstrual"

@implementation MenstrualMessage

- (NSString *)getPath {
    return PATH;
}

- (void)notificationListen {
    __weak typeof(self)weakSelf = self;
    //    15.4 通知事件ID 为固定的 3844
    [[UTEDeviceMgr sharedInstance].physiological onNotifyPhysiological:^(NSDictionary *uteDict) {
        NSDictionary *data = [[NSDictionary alloc] initWithObjectsAndKeys:
                              PATH, KEY_PATH,
                              M_setAppMenstrualModifyTime, KEY_METHOD, nil];

        [weakSelf.eventHander publishEvent:data async:YES];
    }];
    
    [[UTEDeviceMgr sharedInstance].physiological onNotifyCycle:^(UTEModelCycleData *model, NSDictionary *uteDict) {
        NSDictionary *data = [[NSDictionary alloc] initWithObjectsAndKeys:
                              PATH, KEY_PATH,
                              M_setCycle, KEY_METHOD, nil];

        [weakSelf.eventHander publishEvent:data async:YES];
    }];
}

- (void)onSetMenstrualNotifyAbility:(NSNumber *)enable result:(FlutterResult _Nonnull)result {
    __weak typeof(self)weakSelf = self;
    [[UTEDeviceMgr sharedInstance].physiological setMenstrualNotifyAbility:[enable intValue] block:^(NSInteger errorCode, NSDictionary *uteDict) {
        result(@([weakSelf isSuccess:errorCode]));
    }];
}

- (UTEModelCycleData *)getCycleData:(NSDictionary * _Nonnull)params {
    NSDictionary *map = [params objectForKey:TYPE_MAP];
    NSArray *modelDict = [map objectForKey:@"menstrualDataList"];
    NSMutableArray<UTEModelMenstrualData *> *listModel = [NSMutableArray new];
    
    UTEModelCycleData *cycleModel = [UTEModelCycleData new];
    cycleModel.historyCycleSize = [[map objectForKey:@"historyCycleSize"] intValue];
    cycleModel.futureCycleSize = [[map objectForKey:@"futureCycleSize"] intValue];
    cycleModel.modifyTime = [[map objectForKey:@"modifyTime"] intValue];
    cycleModel.manualMenstrualDays = [[map objectForKey:@"manualMenstrualDays"] intValue];
    cycleModel.manualCycleDays = [[map objectForKey:@"manualCycleDays"] intValue];
    
    for(id obj in modelDict) {
        UTEModelMenstrualData *model = [UTEModelMenstrualData new];
        model.menstrualStartTime = [[obj objectForKey:@"mMenstrualStartTime"] intValue];
        model.menstrualEndTime = [[obj objectForKey:@"mMenstrualEndTime"] intValue];
        model.easyToPregnantStartTime = [[obj objectForKey:@"mEasyToPregnantStartTime"] intValue];
        model.easyToPregnantEndTime = [[obj objectForKey:@"mEasyToPregnantEndTime"] intValue];
        model.cycleDays = [[obj objectForKey:@"mCycleDays"] intValue];
        model.isManual = [[obj objectForKey:@"mIsManual"] intValue];
        
        [listModel addObject:model];
    }
    
    cycleModel.menstrualDataList = listModel;
    return cycleModel;
}

- (BOOL)methodCallConsumed:(NSString *)method params:(NSDictionary *)params result:(FlutterResult)result {
    BOOL consumed = YES;
    if ([super methodCallConsumed:method params:params result:result])
        return YES;
    
    assert(method != nil);
    
    __weak typeof(self)weakSelf = self;
    NSNumber *number = nil;
    
    if ([method isEqualToString: M_setMenstrualNotifyAbility]) {
        if (![self isConnected]) {
            result(@NO);
            return YES;
        }
        
        NSDictionary *map = [params objectForKey:TYPE_MAP];
        
        /** 方法描述：下发女性生理周期开关
        50.1  3201 开关是否开启(0:关， 1:开)， Bit 表示
        Bit 位定义说明：
        Bit0： 激活开关。
        Bit1： 经期开始消息提醒开关。
        Bit2： 经期结束消息提醒开关。
        Bit3： 易孕期开始消息提醒开关。
        Bit4： 易孕期结束消息提醒开关。
         **/
        if ([map objectForKey:KEY_ENABLE] != nil) {
            BOOL enable = [map objectForKey:KEY_ENABLE];
            NSNumber *n = enable ? @31 : @0;
            [[UTEDeviceMgr sharedInstance].physiological setMenstrualNotifyAbility:[n intValue] block:^(NSInteger errorCode, NSDictionary *uteDict) {
                result(@([weakSelf isSuccess:errorCode]));
            }];
        }
    } else if ([method isEqualToString: M_setMenstrualCycleData]) {
        if (![self isConnected]) {
            result(@NO);
            return YES;
        }
        
        UTEModelCycleData * cycleModel = [weakSelf getCycleData:params];
        [[UTEDeviceMgr sharedInstance].physiological setCycleModel:cycleModel block:^(NSInteger errorCode, NSDictionary *uteDict) {
            result(@([weakSelf isSuccess:errorCode]));
        }];
    } else if ([method isEqualToString: M_setCycle]) {
        if (![self isConnected]) {
            result(@NO);
            return YES;
        }
        
        NSDictionary *map = [params objectForKey:TYPE_MAP];
        [[UTEDeviceMgr sharedInstance].physiological setCycle:map block:^(NSInteger errorCode, NSDictionary *uteDict) {
            result(@([weakSelf isSuccess:errorCode]));
        }];
    } else if ([method isEqualToString: M_setAppMenstrualCapability]) {
        if (![self isConnected]) {
            result(@NO);
            return YES;
        }
        
        NSInteger capability = 2;
        if (![[params objectForKey:KEY_VALUE] isEmpty]) {
            number = [params objectForKey:KEY_VALUE];
            capability = [number intValue];
        }
        [[UTEDeviceMgr sharedInstance].physiological setMenstrualCapability:capability block:^(NSInteger errorCode, NSDictionary *uteDict) {
            result(@([weakSelf isSuccess:errorCode]));
        }];
    } else if ([method isEqualToString: M_setAppMenstrualModifyTime]) {
        if (![self isConnected]) {
            result(@NO);
            return YES;
        }
        
        NSInteger time = [[NSDate date] timeIntervalSince1970];
        if ([params objectForKey:KEY_VALUE] != nil) {
            time = [[params objectForKey:KEY_VALUE] intValue];
        }
        
        [[UTEDeviceMgr sharedInstance].physiological setPhysiologicalTime:time block:^(NSInteger errorCode, NSDictionary *uteDict) {
            result(@([weakSelf isSuccess:errorCode]));
        }];
    } else {
        consumed = NO;
    }
    
    return consumed;
}
@end
